package org.millioncall.yueyoga.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.millioncall.yueyoga.admin.model.User;
import org.millioncall.yueyoga.admin.model.UserDetail;
import org.millioncall.yueyoga.admin.service.UserDetailService;
import org.millioncall.yueyoga.common.GlobalConstants;
import org.millioncall.yueyoga.common.GlobalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;

public class BaseAction {
	@Autowired
	private UserDetailService userDetailService;

	public User getUser(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute(
				GlobalConstants.LOGIN_USER_KEY);
		return user;
	}

	/**
	 * 初始化header里面的变量
	 * 
	 * @param model
	 */
	protected void initFrontHeader(HttpServletRequest request, ModelMap model) {
		GlobalService.initBasicData(request, model);

		// 注入当前登录用户
		User sessionUser = getUser(request);
		if (sessionUser != null) {
			model.put("sessionUser", sessionUser);
			HttpSession session = request.getSession();
			UserDetail ud = (UserDetail) session
					.getAttribute("LOGIN_USER_DETAIL");
			if (ud == null) {
				ud = this.userDetailService.findUserDetailById(sessionUser
						.getId());
				session.setAttribute("LOGIN_USER_DETAIL", ud);
			}
			model.put("sessionUserInfo", ud);
		}
	}

	public UserDetailService getUserDetailService() {
		return userDetailService;
	}

	public void setUserDetailService(UserDetailService userDetailService) {
		this.userDetailService = userDetailService;
	}
}